package com.misc.tunewsreader;

class NNTPPost {
	private NNTPArticleHeader header;
	private String data;
	private int layer;

	public void setData(String body) {
		data = body;
	}

	public void setHeader(NNTPArticleHeader head) {
		header = head;
	}

	public void setLayer(int level) {
		layer = level;
	}

	public String getData() {
		if (data == null || data.trim() == "") {
			data = NNTPClient.getBody(header.getThreadId());
		}
		return data;
	}

	private String formatQuotes(String data, int level) {
		boolean found_quote = false;
		boolean quote = false;
		String[] lines = data.split("\r\n");
		//String html = "";
		StringBuilder html_b = new StringBuilder();
		String[] color = new String[] { "#0000dd", "#dd00dd", "#00dd00",
				"#dddd00", "#dd0000" };

		for (int i = 0; i < lines.length; i++) {
			if (lines[i].startsWith(">")) {
				lines[i] = lines[i].substring(1);// .trim();
				if (!quote) {
					html_b.append("<blockquote style=\"border-left-color: "
							+ color[level % color.length] + ";\">\r\n"
							+ lines[i] + "\r\n");
					quote = true;
					found_quote = true;
				} else {
					html_b.append(lines[i] + "\r\n");
				}
			} else {
				if (quote) {
					quote = false;
					html_b.append("</blockquote>\r\n");
				}
				html_b.append(lines[i] + "\r\n");
			}
		}

		if (found_quote)
			return formatQuotes(html_b.toString(), level + 1);
		else
			return html_b.toString();
	}

	private String formatQuotes(String data) {
		return formatQuotes(data, 0);
	}

	public static boolean isSignatureStart(String str) {
		if (str.trim().equals("--") && str.startsWith("-- "))
			return true; // default signature starting
		if (str.trim().startsWith("--")) { // 2 or more dashes
			for (int i = 0; i < str.trim().length(); i++) {
				if (str.charAt(i) != '-')
					return false;
			}
			return str.endsWith(" ");
		}
		return false;
	}

	public String getDataHTML() {
		String[] lines = getData().split("\r\n");
		String html = "";
		StringBuilder html_b = new StringBuilder();
		boolean has_signature = false;

		for (int i = 0; i < lines.length; i++) {
			// remove html
			lines[i] = lines[i].replace("<", "&lt;");
			// start of signature
			if (NNTPPost.isSignatureStart(lines[i])) {
				has_signature = true;
				html_b.append("<font color='#808080'>");
			}
			// remove .. at the beginning
			if (lines[i].startsWith(".."))
				lines[i] = lines[i].substring(1);
			// add line breaks
			html_b.append(lines[i] + " <br />\r\n");
		}

		html = formatQuotes(html_b.toString());

		html = "<html><head><style type=\"text/css\">blockquote {border-left: 3px solid blue; padding-left: 8px; margin-left: 2px; margin-right: 2px;}\r\nbody {font-family: %1; font-size: %2;}\r\n.attachment {text-decoration: none; color: #000; font-weight: bold; background-color: #eee; border: 1px solid #ccc;};\r\n</style></head><body>\r\n"
				+ html;

		if (has_signature)
			html += "</font>";

		html += "</body></html>";
		return html;
	}

	public NNTPArticleHeader getHeader() {
		return header;
	}

	public int getLayer() {
		return layer;
	}

	public boolean isRead() {
		return header.isRead();
	}

	public void setRead(boolean is_read) {
		header.setRead(is_read);
	}
}
